<template>
  <div class="honors-overview">
    <div class="page-header">
      <h1 class="page-title">人才计划 / 荣誉称号</h1>
      <p class="page-subtitle">查看全校教职工的人才计划与荣誉称号</p>
    </div>

    <el-card>
      <!-- 筛选栏 -->
      <div class="filter-bar">
        <el-select v-model="filterCategory" placeholder="类别" style="width: 150px;" clearable @change="handleFilterChange">
          <el-option label="全部" value="" />
          <el-option label="人才计划" value="人才计划" />
          <el-option label="荣誉称号" value="荣誉称号" />
        </el-select>

        <el-input
          v-model="filterTeacher"
          placeholder="按教职工姓名筛选"
          style="width: 220px;"
          clearable
          @input="handleFilterChange"
        />

        <el-input
          v-model="searchKeyword"
          placeholder="搜索称号或授予单位"
          style="width: 260px;"
          clearable
          @input="handleFilterChange"
        />
      </div>

      <!-- 列表 -->
      <el-table :data="filteredList" v-loading="loading" stripe>
        <el-table-column prop="userName" label="教职工" width="120" show-overflow-tooltip />
        <el-table-column prop="category" label="类别" width="100" />
        <el-table-column prop="titleFullName" label="称号全称" min-width="240" show-overflow-tooltip />
        <el-table-column prop="grantingOrganization" label="授予单位" min-width="180" show-overflow-tooltip />
        <el-table-column prop="selectionDate" label="入选时间" width="120" />
        <el-table-column label="有效期" min-width="180">
          <template #default="{ row }">
            <span v-if="row.isPermanent">长期有效</span>
            <span v-else>{{ row.validFrom || '-' }} ~ {{ row.validTo || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="160">
          <template #default="{ row }">{{ formatDateTime(row.createdAt) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 查看对话框 -->
    <el-dialog v-model="showView" title="查看称号" width="700px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="教职工">{{ viewData?.userName }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ viewData?.department }}</el-descriptions-item>
        <el-descriptions-item label="类别">{{ viewData?.honor?.category }}</el-descriptions-item>
        <el-descriptions-item label="称号全称">{{ viewData?.honor?.titleFullName }}</el-descriptions-item>
        <el-descriptions-item label="授予单位">{{ viewData?.honor?.grantingOrganization }}</el-descriptions-item>
        <el-descriptions-item label="入选时间">{{ viewData?.honor?.selectionDate }}</el-descriptions-item>
        <el-descriptions-item label="有效期">
          <template #default>
            <span v-if="viewData?.honor?.isPermanent">长期有效</span>
            <span v-else>{{ viewData?.honor?.validFrom || '-' }} ~ {{ viewData?.honor?.validTo || '-' }}</span>
          </template>
        </el-descriptions-item>
        <el-descriptions-item label="备注">{{ viewData?.honor?.notes || '-' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="showView = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { getAllHonors, getHonor } from '@/api/honor'

const loading = ref(false)
const list = ref([])
const showView = ref(false)
const viewData = ref(null)

const filterCategory = ref('')
const filterTeacher = ref('')
const searchKeyword = ref('')

const pagination = reactive({ page: 1, size: 20, total: 0 })

const filteredList = computed(() => {
  let data = list.value
  if (filterCategory.value) data = data.filter(i => i.category === filterCategory.value)
  if (filterTeacher.value) {
    const name = filterTeacher.value.toLowerCase()
    data = data.filter(i => (i.userName || '').toLowerCase().includes(name))
  }
  if (searchKeyword.value) {
    const kw = searchKeyword.value.toLowerCase()
    data = data.filter(i =>
      (i.titleFullName && i.titleFullName.toLowerCase().includes(kw)) ||
      (i.grantingOrganization && i.grantingOrganization.toLowerCase().includes(kw))
    )
  }
  pagination.total = data.length
  const start = (pagination.page - 1) * pagination.size
  const end = start + pagination.size
  return data.slice(start, end)
})

const formatDateTime = (v) => {
  if (!v) return ''
  const d = new Date(v)
  const y = d.getFullYear(), m = String(d.getMonth()+1).padStart(2,'0'), da = String(d.getDate()).padStart(2,'0')
  const hh = String(d.getHours()).padStart(2,'0'), mm = String(d.getMinutes()).padStart(2,'0')
  return `${y}-${m}-${da} ${hh}:${mm}`
}

const handleSizeChange = (size) => { pagination.size = size }
const handleCurrentChange = (page) => { pagination.page = page }
const handleFilterChange = () => { pagination.page = 1 }

const fetchList = async () => {
  try {
    loading.value = true
    const res = await getAllHonors()
    list.value = Array.isArray(res) ? res : (res?.data || [])
  } catch (e) {
    console.error(e)
    ElMessage.error('获取列表失败')
  } finally {
    loading.value = false
  }
}

const handleView = async (row) => {
  try {
    const res = await getHonor(row.id)
    viewData.value = res?.data || res || { honor: row }
    showView.value = true
  } catch (e) {
    console.error(e)
    ElMessage.error('获取详情失败')
  }
}

fetchList()
</script>

<style scoped>
.honors-overview { }
.page-header { margin-bottom: 16px; }
.page-title { margin: 0; font-size: 20px; }
.page-subtitle { margin: 4px 0 0 0; color: #666; }
.filter-bar { display: flex; gap: 12px; margin-bottom: 12px; }
.pagination { margin-top: 12px; display: flex; justify-content: flex-end; }
</style> 